package org.despair2D.ui.puppet 
{
	import flash.display.DisplayObject;
	import org.despair2D.renderer.anime.Animator;
	import org.despair2D.renderer.anime.BitmapInfo;
	import org.despair2D.renderer.IView;
	import org.despair2D.ui.core.ComponentProxy;
	import org.despair2D.ui.events.VisibleEvent;
	import org.despair2D.ui.puppet.supportClasses.AutoSmoothingBitmap;
	import org.despair2D.ui.puppet.supportClasses.ImagePuppetComp;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
final public class AnimatorPuppet extends ComponentProxy
{
	
	public function AnimatorPuppet( sectionName:String )
	{
		m_view = ImagePuppetComp.getImagePuppetComp()
		m_view.proxy = this
		m_img = m_view.m_img
		m_animator = new Animator()
		m_animator.section = sectionName
		m_animator.changeObserver.addListener(__onChange)
		
		this.addEventListener(VisibleEvent.VISIBLE_CHANGE, ____onVisibleChange)
	}
	
	
	/** 动画器 **/
	final public function get animator() : Animator { return m_animator }
	
	
	final ns_despair function __onChange() : void
	{
		var bitmapInfo:BitmapInfo
		
		bitmapInfo = m_animator.bitmapInfo
		if (bitmapInfo)
		{
			m_img.x           =  bitmapInfo.tx
			m_img.y           =  bitmapInfo.ty
			m_img.bitmapData  =  bitmapInfo.bmd;
		}
	}
	
	final override ns_despair function get view() : IView
	{
		return m_view;
	}
	
	final override ns_despair function get shell() : DisplayObject
	{
		return m_view;
	}
	
	final override ns_despair function dispose() : void
	{
		m_animator.dispose();
		m_animator = null
		super.dispose();
		m_view.recycle()
		m_view = null
		m_img = null
	}

	ns_despair function ____onVisibleChange( e:VisibleEvent ) : void
	{
		m_animator.paused = !e.visible
	}
	
	
	ns_despair var m_view:ImagePuppetComp
	
	ns_despair var m_animator:Animator
	
	ns_despair var m_img:AutoSmoothingBitmap
}
}